//
//  NewFeatureCollectionViewController.swift
//  SinaMicroblog
//
//  Created by yuanyuan on 15/12/16.
//  Copyright © 2015年 yuanyuan. All rights reserved.
//

import UIKit
import SnapKit
private let reuseIdentifier = "Cell"
private let maxImageCount = 4



class NewFeatureCollectionViewController: UICollectionViewController {
    
    
    init() {
        let layout = UICollectionViewFlowLayout()
        
        
        //        修改layout的布局
        
        layout.itemSize = UIScreen.mainScreen().bounds.size
        //        修改滚动条
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        //        修改左右的间隔
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        
        super.init(collectionViewLayout: layout)
        //     super.init是构造的最后一步
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
        
        //        取消弹簧
        collectionView?.bounces = false
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        //绑定cell   oc中[UICollectionViewCell Class]
        //        swift中用UICollectionViewCell.self
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    
    
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return maxImageCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        //        真实的身份是NewFeatureCell
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
        
        // Configure the cell
        
        //        测试代码
        //        cell.backgroundColor = indexPath.item % 2 == 0 ? UIColor.redColor():UIColor.yellowColor()
        
        cell.index = indexPath.item
        
        return cell
    }
    
    
    
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        //    cell完成展示之后 indexPath对应的是上一个cell indexPath  不能使用indexpath来获取cell
        print(indexPath.item)
        //        获取准确的cell
        let cell = collectionView.visibleCells().last! as! NewFeatureCell
        //        获取cell的indexpath
        let index = collectionView.indexPathForCell(cell)!
        //        判断index是不是3   执行动画
        if index.item == maxImageCount - 1 {
            //        执行动画
            cell.startAnimation()
            
            
        }
        
        
    }
    
    
}


private func startBtnDidClick() {
    
    print(__FUNCTION__)
    
    
}

//在一个文件中可以同时建多个类
class NewFeatureCell:UICollectionViewCell {
    
    //    添加index属性
    var index:Int = 0 {
        //        oc中的写法是一样一样的
        //        修改cell对应显示的image
        didSet {
            //
            iconView.image = UIImage(named: "new_feature_\(index+1)")
            //    隐藏开始按钮
            //        startBtn.hidden = index != 3
            startBtn.hidden = true
            
        }
        
        
    }
    
    
    
    //    重写构造方法  Cell构造的入口
    override init(frame: CGRect) {
        super.init(frame: frame)
        print(frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //    设置界面UI
    private func setupUI() {
        //    添加子视图
        //        将自定义的子视图添加到contentView上
        contentView.addSubview(iconView)
        contentView.addSubview(startBtn)
        //        使用snapkit添加约束
        //        snp_makeconstraint表示添加约束
        iconView.snp_makeConstraints { (make) -> Void in
            //            make表示需要添加的约束对象
            make.edges.equalTo(contentView.snp_edges)
            
        }
        
        startBtn.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(contentView.snp_centerX)
            //            添加底部约束
            make.bottom.equalTo(contentView.snp_bottom).offset(-180)
        }
        //    添加按钮的点击事件
        startBtn.addTarget(self, action: "startBtnDidClick", forControlEvents: .TouchUpInside)
        
    }
    
    
    func startAnimation() {
        //        动画之前隐藏按钮
        startBtn.hidden = false
        //        设置初始形变
        startBtn.transform = CGAffineTransformMakeScale(0, 0)
        //      usingSpringWithDamping:弹簧系数 0.1~1越小越弹 options:动画的可选项 swift使用数组
        //        initialSpringVelosity :加速度  animation:执行动画闭包
        UIView.animateWithDuration(1.6, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: [], animations: { () -> Void in
            
            self.startBtn.transform = CGAffineTransformIdentity
            
            }) { (_) -> Void in
                print("ok")
        }
        
        
    }
    
    
    //分析视图中的所有子视图 并且懒加载出所有的子视图
    private lazy var iconView:UIImageView=UIImageView(image:UIImage(named:"ad_background"))
    //    懒加载开始按钮
    private lazy var startBtn:UIButton = {
        
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState:.Normal)
        btn.setBackgroundImage(UIImage(named:"new_feature_finish_button_highlighted"), forState:.Highlighted)
        
        btn.setTitle("开始体验", forState: .Normal)
        //        设置大小
        btn.sizeToFit()
        
        return btn
        
    }()
    
    
    
}